pub use sea_orm::entity::prelude::*;
use sea_orm::sea_query::ForeignKeyAction;

use crate::{add_relate, booking_status, user::custom, views::view_custom};

#[derive(Debug, Clone, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "booking_room")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    /// 预约指定日期的指定会议室
    pub book_day_id: i64,
    /// 预约用户
    pub custom_id: i64,
    /// 预约时间段
    pub time_pice: i64,
}

#[derive(Debug, Clone, Copy, EnumIter)]
pub enum Relation {
    BookingStatus,
    Custom,
    ViewCustom,
}

impl RelationTrait for Relation {
    fn def(&self) -> RelationDef {
        match self {
            Relation::BookingStatus => Entity::belongs_to(booking_status::Entity)
                .from(Column::BookDayId)
                .to(booking_status::Column::Id)
                .on_update(ForeignKeyAction::Cascade)
                .on_delete(ForeignKeyAction::Restrict)
                .into(),

            Relation::Custom => Entity::belongs_to(custom::Entity)
                .from(Column::CustomId)
                .to(custom::Column::Id)
                .on_update(ForeignKeyAction::Cascade)
                .on_delete(ForeignKeyAction::Restrict)
                .into(),
            Relation::ViewCustom => Entity::belongs_to(view_custom::Entity)
                .from(Column::CustomId)
                .to(view_custom::Column::Uid)
                .on_update(ForeignKeyAction::Cascade)
                .on_delete(ForeignKeyAction::Restrict)
                .into(),
        }
    }
}

add_relate! {
    booking_status::Entity => Entity | Relation::BookingStatus,
    custom::Entity => Entity | Relation::Custom
}

impl ActiveModelBehavior for ActiveModel {}
